package com.ambow.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.entity.Product;
import com.ambow.service.ProductService;
import com.ambow.service.impl.ProductServiceImpl;

/**
 * Servlet implementation class ProductServlet
 */
@WebServlet("/ProductServlet")
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ProductService productService=new ProductServiceImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		
		
		int a=0;
		String status=request.getParameter("status");
		
		System.out.println( status );
		
		if(status.equals("queryAllT")) {
			queryAllT(request,response);
		}else if(status.equals("queryOne")) {
			queryOne(request,response);
		}else if(status.equals("queryAllL")) {
			queryAllL(request, response);
		}else if(status.equals("queryAllB")) {
			queryAllB(request,response);
		}
	}
	/**
	 * @param request
	 * @param response
	 *@author Administrator
	 *@date 2017年12月24日 下午3:45:15
	 */
	public void queryAllB(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String brand=request.getParameter("brand");
		String kind=request.getParameter("kind");
		String sql="select * from Product  where brand='"+brand+"'"+"and kind='"+kind+"'"+"order by rand()  limit 9";
		 List<Product>  list=productService.getRand(sql);
		 System.out.println("llllllllllllllll333333"+list.size());
		 List para = new ArrayList();
		 para.add(brand);
		 para.add(kind);
		 request.setAttribute("list", list);
		 request.setAttribute("para", para);
		 request.getRequestDispatcher("pages/product.jsp").forward(request, response);
		
	}
	//select * from Product where type='太阳眼镜'and kind='男性' order by rand() limit 9;
	public void queryAllT(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String kind=request.getParameter("kind");
		String type=request.getParameter("type");
		String sql="select * from Product where type='"+type+"'"+"and kind='"+kind+"'"+"order by rand()  limit 9";
		 List<Product>  list=productService.getRand(sql);
		 System.out.println("llll2222233333llllllllllllllllllllllllllllllll22222"+list.size());
		 List para = new ArrayList();
		 para.add(type);
		 para.add(kind);
		 request.setAttribute("list", list);
		 request.setAttribute("para", para);
		 request.getRequestDispatcher("pages/product.jsp").forward(request, response);
		
	}
	public void queryAllL(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String label=request.getParameter("label");
		String kind=request.getParameter("kind");
		String sql="select * from Product  where label='"+label+"'"+"and kind='"+kind+"'"+"order by rand()  limit 9";
		List<Product>  list=productService.getRand(sql);
		 System.out.println("llllllllllllllllllllllllllllllllllllll22222"+list.size());
		 List para = new ArrayList();
		 para.add(label);
		 para.add(kind);
		 request.setAttribute("list", list);
		 request.setAttribute("para", para);
		 request.getRequestDispatcher("pages/product.jsp").forward(request, response);
		
	}
	public void queryOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.valueOf(request.getParameter("id"));
		String sql="select * from Product where id="+id;
		Product product=productService.QueryOne(sql);
		request.setAttribute("product", product);
		request.getRequestDispatcher("pages/single.jsp").forward(request, response);
		
	}
	

}
